package com.test.project.system.shiti.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.test.common.utils.poi.ExcelUtil;
import com.test.framework.web.controller.BaseController;
import com.test.framework.web.domain.AjaxResult;
import com.test.framework.web.page.TableDataInfo;
import com.test.project.system.dati.domain.TDati;
import com.test.project.system.dati.service.ITDatiService;
import com.test.project.system.shiti.domain.TShiti;
import com.test.project.system.shiti.service.ITShitiService;
import com.test.project.system.wenjuan.domain.TWenjuan;
import com.test.project.system.wenjuan.service.ITWenjuanService;

/**
 * 试题管理Controller
 * 
 * @author test
 * @date 2023-12-05
 */
@Controller
@RequestMapping("/system/shiti")
public class TShitiController extends BaseController
{
    private String prefix = "system/shiti";

    @Autowired
    private ITShitiService tShitiService;
    @Autowired
    private ITWenjuanService tSectionService; 
    

    @Autowired
    private ITDatiService tDatiService;
    @GetMapping()
    public String shiti()
    {
        return prefix + "/shiti";
    }
    @GetMapping("/st")
    public String st()
    {
    	return prefix + "/st";
    }
    @GetMapping("/statistic")
    public String statistic(@RequestParam Long pid,ModelMap mmap)
    {
    	TDati dat = new TDati();
    	dat.setTimuId(pid);
    	List<TDati> selectTDatiList = tDatiService.selectTDatiList(dat);
    	int a = 0;
    	int b = 0;
    	int c = 0;
    	int d = 0;
    	for(TDati e : selectTDatiList) {
    		if(e.getContent().equals("A")) {
    			a++;
    		}
    		if(e.getContent().equals("B")) {
    			b++;
    		}
    		if(e.getContent().equals("C")) {
    			c++;
    		}
    		if(e.getContent().equals("D")) {
    			d++;
    		}
    	}
    	Map<String,Integer> rese = new HashMap();
    	rese.put("A", a);
    	rese.put("B", b);
    	rese.put("C", c);
    	rese.put("D", d);
    	mmap.put("res", rese);
    	return prefix + "/statistic";
    }

    /**
     * 查询试题管理列表
     */
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(TShiti tShiti)
    {
        startPage();
        List<TShiti> list = tShitiService.selectTShitiList(tShiti);
        return getDataTable(list);
    }
    @PostMapping("/importData")
    @ResponseBody
    public AjaxResult importData(MultipartFile file) throws Exception
    {
        ExcelUtil<TShiti> util = new ExcelUtil<TShiti>(TShiti.class);
        List<TShiti> userList = util.importExcel(file.getInputStream());
        String message = tShitiService.importShjiti(userList);
        return AjaxResult.success(message);
    }
    /**
     * 导出试题管理列表
     */
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(TShiti tShiti)
    {
        List<TShiti> list = tShitiService.selectTShitiList(tShiti);
        ExcelUtil<TShiti> util = new ExcelUtil<TShiti>(TShiti.class);
        return util.exportExcel(list, "shiti");
    }

    /**
     * 新增试题管理
     */
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {
    	List<TWenjuan> list = tSectionService.selectTWenjuanList(new TWenjuan());
    	mmap.put("list", list);
        return prefix + "/add";
    }

    /**
     * 新增保存试题管理
     */
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(TShiti tShiti)
    {
        return toAjax(tShitiService.insertTShiti(tShiti));
    }

    /**
     * 修改试题管理
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        TShiti tShiti = tShitiService.selectTShitiById(id);
        mmap.put("tShiti", tShiti);
        return prefix + "/edit";
    }

    /**
     * 修改保存试题管理
     */
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(TShiti tShiti)
    {
        return toAjax(tShitiService.updateTShiti(tShiti));
    }

    /**
     * 删除试题管理
     */
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(tShitiService.deleteTShitiByIds(ids));
    }
}
